package com.moarea.app.service.impl.dictionary;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.moarea.app.dao.BaseDictionaryMapper;
import com.moarea.app.lang.model.ResultObject;
import com.moarea.app.lang.utils.StringUtils;
import com.moarea.app.model.BaseDictionary;
import com.moarea.app.mybatis.model.PagedObject;
import com.moarea.app.mybatis.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Service("baseDictionaryService")
public class BaseDictionaryService extends BaseService<BaseDictionary> {

    @Autowired
    private BaseDictionaryMapper baseDictionaryMapper;

    @Override
    public Mapper<BaseDictionary> getMapper() {
        return this.baseDictionaryMapper;
    }

    /**
     * 根据父节点查询所有的子节点
     *
     * @param param 查询条件 name可以为空
     * @return
     */
    public List<Map<String, Object>> selectByPcode(Map<String, Object> param) {
        String pcode = param.get("pcode").toString();
        List<Map<String, Object>> list = baseDictionaryMapper.selectByPcode(pcode);
        return list;
    }

    /**
     * 根据父节点查询所有的子节点，沒有code
     *
     * @param param 查询条件 name可以为空
     * @return
     */
    public List<Map<String, Object>> selectByPcodeNoCode(Map<String, Object> param) {
        String pcode = param.get("pcode").toString();
        String name = null;
        if (StringUtils.isNotBlank(param, "name")) {
            name = StringUtils.addLike(param.get("name"));
        }
        List<Map<String, Object>> list = baseDictionaryMapper.selectByPcodeNoCode(pcode, name);
        return list;
    }

    public List<Map<String, Object>> selectByID(Map<String, Object> param) {
        String ID = param.get("ID").toString();
        List<Map<String, Object>> list = baseDictionaryMapper.selectByID(ID);
        return list;
    }


    /**
     * 查询数据字典所以数据
     *
     * @param session
     * @return
     */
    public ResultObject listTreeMenus(HttpSession session) {

        return ResultObject.createInstance(true, "", baseDictionaryMapper.listTreeMenus());
    }


    public PagedObject<List<BaseDictionary>> selectChildBypId(Map<String, Object> param, int pageNum, int pageSize, int count) {


        Example example = new Example(BaseDictionary.class);
        Example.Criteria criteria = example.createCriteria();

        Page<BaseDictionary> page = PageHelper.startPage(pageNum, pageSize, count < 0);

        List<BaseDictionary> baseDictionaries = baseDictionaryMapper.selectChildrenByPid(param.get("id") == null ? "" : param.get("id").toString());

        return new PagedObject<List<BaseDictionary>>(
                baseDictionaries, count < 0 ? (int) page.getTotal() : count);

    }

    public List<Map<String, Object>> selectSupByType(String corp_name) {

        return baseDictionaryMapper.selectSupByType(corp_name);
    }


    public ResultObject selectMaxSeqByPID(Map<String, Object> param, HttpSession session) {

        Integer maxSeq = baseDictionaryMapper.selectMaxSeqByPID(param.get("PID") == null ? "" : param.get("PID").toString());
        if (maxSeq == null) {
            return ResultObject.createInstance(true, "", "1");
        } else {
            return ResultObject.createInstance(true, "", String.valueOf(maxSeq + 1));
        }
    }

    /**
     * 根据大类Pid查部类ID
     *
     * @param pid
     * @param session
     * @return
     */
    public String selectCategoryByBigPID(String pid, HttpSession session) {
        return baseDictionaryMapper.selectFaIDByChildPID(pid);
    }

    /**
     * 通過ID查paraname
     *
     * @param ID
     * @param session
     * @return
     */
    public String seleParanameByID(String ID, HttpSession session) {
        return baseDictionaryMapper.seleParanameByID(ID);
    }

    /**
     * 根据父节点编号和子节点编号查询某个节点
     *
     * @param param
     * @param session
     * @return
     */
    public ResultObject selectNodeBySupAndSubPcode(Map<String, Object> param, HttpSession session) {
        return ResultObject.createInstance(true, "成功", baseDictionaryMapper.selectNodeBySupAndSubPcode(param.get("SUP_CODE").toString(), param.get("SUB_CODE").toString()));
    }

    //验证编码是否重复跟（新增）
    public Integer isRepeatByPID(Map<String, Object> map) {
        return baseDictionaryMapper.isRepeatByPID(map);
    }

    //验证编码是否重复（更新）
    public Integer isRepeatByPIDAndID(Map<String, Object> map) {
        return baseDictionaryMapper.isRepeatByPIDAndID(map);
    }


    /**
     * 根据父类id删除子类，递归
     */
    public void deleteByID(String ID,HttpSession session) {
        //根据id查询子节点，
        List<BaseDictionary> baseChild = baseDictionaryMapper.selectChildrenByPid(ID);
        //删除父类
        baseDictionaryMapper.deleteByID(ID);
        for (BaseDictionary bChildData : baseChild) {
            deleteByID(bChildData.getID(),session);
        }
    }
}
